DAMN = (typeof DAMN != "undefined")?  DAMN : {};

DAMN.Formats = new function() 
{   
  this.AttachTo = function(parent) {
    parent.tabs('add', '#tabs-Formats', "Formats");
    this.tab = $("#tabs-Formats");
    
    DAMN.AssetMananger.Register("asset", this.Formats.bind(this));
	};
	
	this.Refresh = function(id, target) 
	{
	  var acc = $('<div id="formats-accordion"></div>');
	  acc.addClass('ui-accordion ui-widget ui-helper-reset ui-accordion-icons');
    target.append(acc);
    
    acc.text("Loading formats...");
    
	   $.getJSON("/assets/"+id+"/formats/", function(data){
        acc.empty();                              
        for(m in data){
          acc.append(new DAMN.Format(data[m], id));
        }
      });
  };

	
	this.Formats = function(op, el) 
  { 
    this.tab.empty();
    if (op == 'SELECT') { 
      if (DAMN.AssetMananger.selected.length == 1) {
        this.Refresh(el.data("assetid"), this.tab);
      }
      else {
       this.tab.append("<p>Multiple selected."+DAMN.AssetMananger.selected.length+"</p>");
      }
    }
    else {
      this.tab.append("<p>Nothing selected.</p>");
    }
  };
  		
};

DAMN.Format = function(format, assetid) {
  var h = new DAMN.MetaData.Header(format.format);
  var i = 0;
  h.empty();
  for (m in format.options) {
    h.append($('<span>'+format.options[m][0]+': '+format.options[m][1]+' (default: '+format.options[m][2]+')</span><br/>'));
    i++;
  }
  if (!i) {
    h.append($('<p>No available options.<p>'));
  }
  h.append($('<span><a href="/'+format.template+'" title="/'+format.template+'">Template<a></span><br/><br/>'));
  
  h.append($('<div style=""><a href="/assets/'+assetid+'/transcode/?download&format='+format.format_esc+'">Transcode<a></div>'));
  
  

  return h.element;
};

